﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.Text;
using System.IO;
using System.Threading;

namespace DnPakExplorer.Forms {
	public partial class Equiper : FormBase {

		Character mCharacter;

		private bool mIsInited;

		public Equiper(Character character) {
			InitializeComponent();
			SetStyle(ControlStyles.ResizeRedraw, true);
			menuStrip1.Renderer = new DnPakExplorer.Controls.ContextMenuStripRenderer();

			mCharacter = character as Character;

			equipmentPanelCtrl1.Init(character);
			glyphCtrl1.Init(character);
			cashEquipmentPanelCtrl1.Init(character);
			talismanEquipementPanelCtrl1.Init(character);
			skillPanelControl1.Character = character;
			bagControl1.Character = character;

			mCharacter.EquipmentPanel.OnPropertyChanged += EquipmentPanel_OnPropertyChanged;
			mCharacter.SkillNanel.OnSkillItemChanged += SkillNanel_OnSkillItemChanged;
			webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
			webBrowser2.DocumentCompleted += webBrowser2_DocumentCompleted;
			webBrowser1.DocumentText = File.ReadAllText(@"..\..\..\WebProperty.html", Encoding.UTF8);
			webBrowser2.DocumentText = File.ReadAllText(@"..\..\..\WebProperty.html", Encoding.UTF8);
			bagControl1.Text = "我的装备";
			myPanel1.SetTabTitle(webBrowser1, "装备信息");
			myPanel1.SetTabTitle(webBrowser2, "角色信息");
			myPanel1.SetTabTitle(skillPanelControl1, "应用的技能");
			myPanel1.SetTabTitle(bagControl1, "我的背包");
			mIsInited = true;
			this.Disposed += Equiper_Disposed;
		}

		void Equiper_Disposed(object sender, EventArgs e) {
			if (mCharacter != null) {
				mCharacter.CloseCharacter();
				mCharacter.EquipmentPanel.OnPropertyChanged -= EquipmentPanel_OnPropertyChanged;
				mCharacter.SkillNanel.OnSkillItemChanged -= SkillNanel_OnSkillItemChanged;
			}
		}


		void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
			SetCharacterPropertyText();
		}

		void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
			SetEquipmentPropertyText();
		}

		void SetEquipmentPropertyText() {
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat(@"<span style=""color:yellow"">[属性]</span><br>", mCharacter.Name);
			for (int i=0; i<(int)PropertyID.ID_MAX; i++) {
				float value = mCharacter.EquipmentPanel.GetProperty((PropertyID)i);
				if (PropertyUtil.IsGenProperty(i) || !PropertyUtil.IsAppliedProperty(i))
					continue;
				else {
					int genProperty = PropertyUtil.GetGenProperty(i);
					if (genProperty > 0) {
						sb.AppendFormat("{0}:\n<span style=\"color:green\">{1}</span>\n+\n<span style=\"color:yellow\">{2}%</span><br>", PropertyUtil.GetPropertyName(i), value, 100*mCharacter.EquipmentPanel.GetProperty((PropertyID)genProperty));
					} else {
						if(PropertyUtil.IsPercentProperty(i))
							sb.AppendFormat("{0}:\n<span style=\"color:green\">{1}%</span><br>", PropertyUtil.GetPropertyName(i), value*100);
						else
							sb.AppendFormat("{0}:\n<span style=\"color:green\">{1}</span><br>", PropertyUtil.GetPropertyName(i), value);
					}

				}
			}
			HtmlDocument htmlDoc = webBrowser1.Document;
			if (htmlDoc.Body != null)
				htmlDoc.Body.InnerHtml = sb.ToString();
		}

		void SetCharacterPropertyText() {
			HtmlDocument htmlDoc = webBrowser2.Document;
			if (htmlDoc.Body == null)
				return;
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("角色：{0}<br>\n", mCharacter.Name);
			sb.AppendFormat("职业：{0}<br>\n", mCharacter.JobName);
			sb.AppendFormat("等级：{0}<br>\n", mCharacter.Level);
			sb.AppendFormat("英雄等级：{0}<br>\n", mCharacter.HeroLevel);
			sb.AppendFormat("<span style=\"color:yellow\">[英雄等级能力]</span><br>\n");
			if (mCharacter.HeroLevel > 0) {
				foreach (Property prop in mCharacter.EquipmentPanel[Character.HeroPropertyID]) {
					sb.Append(prop.ToString()+"<br>");
				}
			}
			Appellation app =  mCharacter.Appellation;
			if (app != null) {
				sb.AppendFormat("<span style=\"color:yellow\">称号：{0}</span><br>\n", app.Name);
				foreach (Property prop in app) {
					sb.Append(prop.ToString()+"<br>");
				}
			}

			sb.AppendFormat("<span style=\"color:yellow\">[属性]</span><br>\n", mCharacter.Name);
			foreach (int i in showProperty) {
				if (PropertyUtil.IsPercentProperty(i)) {
					sb.AppendFormat(PropertyUtil.GetPropertyName(i)+"：")
					.AppendFormat("<span style=\"color:white\">{0:#0.##%}</span>\n", mCharacter.GetWhiteProperty((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">+</span>\n")
					.AppendFormat("<span style=\"color:green\">{0:#0.##%}</span>\n", mCharacter.GetGreenProperty((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">+</span>\n")
					.AppendFormat("<span style=\"color:#9966FF\">{0:#0.##%}</span>\n", mCharacter.GetBluePropery((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">={0:#0.##%}</span>\n", mCharacter.GetProperty((PropertyID)i));
				} else {
					sb.AppendFormat(PropertyUtil.GetPropertyName(i)+"：")
					.AppendFormat("<span style=\"color:white\">{0:F0}</span>\n", mCharacter.GetWhiteProperty((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">+</span>\n")
					.AppendFormat("<span style=\"color:green\">{0:F0}</span>\n", mCharacter.GetGreenProperty((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">+</span>\n")
					.AppendFormat("<span style=\"color:#9966FF\">{0:F0}</span>\n", mCharacter.GetBluePropery((PropertyID)i))
					.AppendFormat("<span style=\"color:white\">={0:F0}</span>\n", mCharacter.GetProperty((PropertyID)i));
				}
				if (mCharacter.IsRateProperty((PropertyID)i)) {
					float skillRate = mCharacter.SkillNanel.GetProperty((PropertyID)i).rateValue;
					if (skillRate != 0) {
						sb.AppendFormat("<span style=\"color:yellow\">[{0:#0.##%}({1:+#0.##%;-#0.##%})]</span>\n",mCharacter.GetRateProperty((PropertyID)i), skillRate);
					} else
						sb.AppendFormat("<span style=\"color:yellow\">[{0:#0.##%}]</span>\n", mCharacter.GetRateProperty((PropertyID)i));
				}
				sb.Append("<br>");
			}

			sb.AppendFormat("<span style=\"color:#f44336\">火攻指数：{0}</span><br>",
				mCharacter.GetProperty(PropertyID.MinMagicAttack)
				*(1+mCharacter.GetProperty(PropertyID.FireAttack))
				*(1+mCharacter.GetRateProperty(PropertyID.Critical)*(2-mCharacter.GetRateProperty(PropertyID.CriticalDamage)))
				*(1+mCharacter.GetRateProperty(PropertyID.FinalDamage))
				);

			sb.AppendFormat("<span style=\"color:#2196f3\">水攻指数：{0}</span><br>",
				mCharacter.GetProperty(PropertyID.MinMagicAttack)
				*(1+mCharacter.GetProperty(PropertyID.WaterAttack))
				*(1+mCharacter.GetRateProperty(PropertyID.Critical)*(2-mCharacter.GetRateProperty(PropertyID.CriticalDamage)))
				*(1+mCharacter.GetRateProperty(PropertyID.FinalDamage))
				);

			sb.AppendFormat("<span style=\"color:#ffeb3b\">光攻指数：{0}</span><br>",
				mCharacter.GetProperty(PropertyID.MinMagicAttack)
				*(1+mCharacter.GetProperty(PropertyID.LightAttack))
				*(1+mCharacter.GetRateProperty(PropertyID.Critical)*(2-mCharacter.GetRateProperty(PropertyID.CriticalDamage)))
				*(1+mCharacter.GetRateProperty(PropertyID.FinalDamage))
				);

			sb.AppendFormat("<span style=\"color:#6dc171\">暗攻指数：{0}</span><br>",
				mCharacter.GetProperty(PropertyID.MinMagicAttack)
				*(1+mCharacter.GetProperty(PropertyID.DarkAttack))
				*(1+mCharacter.GetRateProperty(PropertyID.Critical)*(2-mCharacter.GetRateProperty(PropertyID.CriticalDamage)))
				*(1+mCharacter.GetRateProperty(PropertyID.FinalDamage))
				);

			sb.AppendFormat("普攻指数：{0}<br>",
				mCharacter.GetProperty(PropertyID.MinMagicAttack)
				*(1+mCharacter.GetRateProperty(PropertyID.Critical)*(2-mCharacter.GetRateProperty(PropertyID.CriticalDamage)))
				*(1+mCharacter.GetRateProperty(PropertyID.FinalDamage))
				);

			sb.AppendFormat("<span style=\"color:yellow\">[技能]</span><br>\n", mCharacter.Name);
			SkillPanel skillPanel = mCharacter.SkillNanel;
			for (int i=0; i<(int)PropertyID.ID_MAX; i++) {
				SkillProperty sp = skillPanel.GetProperty((PropertyID)i);
				if (sp.value!=0 || sp.percentValue!=0 || sp.rateValue!=0) {
					sb.AppendFormat(PropertyUtil.GetPropertyName(i)+"：")
						.AppendFormat("<span style=\"color:white\">{0:F0}</span>\n", sp.value)
						.AppendFormat("<span style=\"color:white\">,</span>\n")
						.AppendFormat("<span style=\"color:green\">{0:#0.##%}</span>\n", sp.percentValue)
						.AppendFormat("<span style=\"color:white\">,</span>\n")
						.AppendFormat("<span style=\"color:yellow\">{0:#0.##%}</span>\n<br>", sp.rateValue);
				}
			}
			htmlDoc.Body.InnerHtml = sb.ToString();
		}

		void EquipmentPanel_OnPropertyChanged() {
			if (mIsInited)
				NotifyEquipmentChanged();
		}

		void SkillNanel_OnSkillItemChanged() {
			if (mIsInited)
				NotifyEquipmentChanged();
		}

		int[] showProperty = new int[]{
			0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, 13, 16, 17, 18, 19, 24, 25, 26, 27, 29,
			100, 103, 105, 106, 107
		};

		public void NotifyEquipmentChanged() {
			SetCharacterPropertyText();
			SetEquipmentPropertyText();
		}

		

		private void Equiper_Load(object sender, EventArgs e) {
		}

		private void OnMenuItemStore(object sender, EventArgs e) {
			new EquipmentStoreViewerForm(Util.GetExplorer()).Show();
		}

		private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e) {
			NotifyEquipmentChanged();
		}

		private void 背包ToolStripMenuItem_Click(object sender, EventArgs e) {
			BagForm bag = new BagForm(mCharacter);
			bag.Show(this);
		}

		private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) {
			DnPlayerUtil.SaveCharacter(mCharacter, true);
			MessageBox.Show("已保存。");
		}

		private void 称号ToolStripMenuItem_Click(object sender, EventArgs e) {
			new AppellationForm().Show(this);
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e) {
			TopMost = checkBox1.Checked;
		}

		private void 装备统计ToolStripMenuItem_Click(object sender, EventArgs e) {
			new EquipementAnalysis().Show(this);
		}

		private void 技能ToolStripMenuItem_Click(object sender, EventArgs e) {
			new SkillPanelForm(mCharacter).Show(this);
		}

	}
}
